//
//  LeanCloudRegisterViewController.m
//  MPWeChatDemo
//
//  Created by HappyMilk 01 on 2017/5/2.
//  Copyright © 2017年 srulos. All rights reserved.
//

#import "LeanCloudRegisterViewController.h"
#import "LoginHeader.h"
#import <AVOSCloud/AVOSCloud.h>

#define LOC_STR(str)  NSLocalizedStringFromTable(str, @"Login", nil)

@interface LeanCloudRegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *username_field;
@property (weak, nonatomic) IBOutlet UITextField *password_field;
@property (weak, nonatomic) IBOutlet UIButton *register_btn;

@end

@implementation LeanCloudRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
    
    ADD_OBSERVER(self, @selector(onAppInfoLoadedCB:), NOTIFICATION_APP_INFO_LOADED, nil);
}

- (void)onAppInfoLoadedCB:(id)sender{
    if([MPWeChat stander].m_needLogin && IS_CHINESE)
      [ self.navigationController popViewControllerAnimated:true];
}


-(void)initViews{
    _register_btn.layer.cornerRadius = 5;
    _register_btn.backgroundColor = self.mainColor;
    self.title = @"Register";
    
    UIImageView *userNameImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_username.png"]];
    userNameImage.frame = CGRectMake(10, 8, 15, 15);
    userNameImage.contentMode = UIViewContentModeScaleAspectFit;
    UIView *userNameLeftView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 25, 30)];
    [userNameLeftView addSubview:userNameImage];
    self.username_field.leftView = userNameLeftView;
    self.username_field.leftViewMode = UITextFieldViewModeAlways;
    
    UIImageView *passwordImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_psd.png"]];
    passwordImage.frame = CGRectMake(10, 8, 15, 15);
    passwordImage.contentMode = UIViewContentModeScaleAspectFit;
    UIView *passwordLeftView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 25, 30)];
    [passwordLeftView addSubview:passwordImage];
    self.password_field.leftView = passwordLeftView;
    self.password_field.leftViewMode = UITextFieldViewModeAlways;
}


- (IBAction)onRegister:(UIButton *)sender {
    NSString *username = _username_field.text;
    NSString *password = _password_field.text;
    
    if(username.length<6 || password.length <6 ){
        NSString *msg = LOC_STR(@"Please fill in the correct account or password! Account password must be more than 6!");
        SHOW_HUD_MESSAGE(msg);
        return;
    }
    SHOW_HUD(@"Is registering...");
    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
    user.username = username;// 设置用户名
    user.password =  password;// 设置密码
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        HIDE_HUD
        if (succeeded) {
            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"" message:LOC_STR(@"Registration success!please sign in") delegate:nil cancelButtonTitle:LOC_STR(@"Confirm") otherButtonTitles:nil, nil];
            [alertview show];
            [self.navigationController popViewControllerAnimated:true];
        } else {
            SHOW_HUD_MESSAGE(@"Registration failed, user name may exist, please try again or return to login");
        }
        
        NSLog(@"%@",error);
    }];
    
}



@end
